// ========================================================
// Des：测试webrequire请求
// Autor：师哥吃你个小鸡腿 
// Email: hf_8220@163.com
// CreateTime：2024/01/11 14:50:57
// 版 本：v 1.0
// ========================================================
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Networking;

public class Demo_WebRequire : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        //StopCoroutine("GetTexture");
        StartCoroutine("GetTexture");
    }

    //web加载图片
    IEnumerator GetTexture()
    {
        using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture("https://tse4-mm.cn.bing.net/th/id/OIP-C.V3ErhDvYHF3xQdAJeRs3owHaEZ?w=214&h=180&c=7&r=0&o=5&pid=1.7"))
        {
            yield return uwr.SendWebRequest();             
            if (uwr.result == UnityWebRequest.Result.ConnectionError || 
            uwr.result == UnityWebRequest.Result.ProtocolError)
            {
                Debug.Log(string.Format("uwr.error:{0}", uwr.error));
            }
            else
            {
                Texture2D texture2D = DownloadHandlerTexture.GetContent(uwr);
                //yield return texture2D;
                Debug.Log(111);
                Debug.Log(texture2D);
                SetCubeTexture(texture2D);

            }
        }
    }

    private void SetCubeTexture(Texture2D texture)
    {
        Debug.Log(222);
        GameObject.Find("Sphere").GetComponent<Renderer>().material.mainTexture = texture;
        Debug.Log(333);
    }
}
